package com.gome.splunk.utils;

import java.io.InputStream;

import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import com.gome.splunk.ModelClass.UpdateErrorMessageInfo;
import com.gome.splunk.UpdateInfo;

public class UpdateUtil {
    public static UpdateInfo getUpdataInfo(InputStream is) throws Exception{     
        XmlPullParser  parser = Xml.newPullParser();       
         parser.setInput(is, "utf-8");//设置解析的数据源       
         int type = parser.getEventType();     
         UpdateInfo info = new UpdateInfo();//实体      
         while(type != XmlPullParser.END_DOCUMENT ){     
            switch (type) {     
            case XmlPullParser.START_TAG:     
                 if("version".equals(parser.getName())){     
                     info.setVersion(parser.nextText()); //获取版本号      
                 }else if ("url".equals(parser.getName())){     
                     info.setUrl(parser.nextText()); //获取要升级的APK文件      
                 }else if ("description".equals(parser.getName())){     
                     info.setDescription(parser.nextText()); //获取该文件的信息      
                 }     
                 break;     
             }     
             type = parser.next();     
         }     
         return info;     
     }

    public static UpdateErrorMessageInfo getUpdateErrorMessageInfo(String resopndString) throws  Exception{
        if(resopndString != null && resopndString != "") {
            UpdateErrorMessageInfo errorInfo = new UpdateErrorMessageInfo();
            JSONObject jsonObject = new JSONObject(resopndString);

            String responseInfo = jsonObject.getString("msg");
            Integer resonseCode = jsonObject.getInt("code");
            boolean success = jsonObject.getBoolean("success");

            errorInfo.setErrorInfo(responseInfo);
            errorInfo.setResponseCode(resonseCode);
            errorInfo.setSuccess(success);

            return errorInfo;
        }
        return  null;
    }
}
